package com.example.test.controller;

import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Enumeration;


@CrossOrigin
@RestController
@RequestMapping(value = "/")
public class LoginController {
    private static final Logger logger = LoggerFactory.getLogger(LoginController.class);
    @Value("${server.port}")
    private String port;

    @RequestMapping(value = "/", produces = "text/html;charset=UTF-8")
    private String hello(HttpServletRequest request, HttpServletResponse response, @RequestBody String body){
        logger.info(body);
        logger.warn(body);
        logger.error(body);

        logger.info("请求来自者："+request.getHeader("Origin"));
        logger.info("请求来自者完整路径："+request.getHeader("Referer"));

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("success", true);
        jsonObject.put("message", "成功！");
        return jsonObject.toString();
    }

    @RequestMapping(value = "/set")
    private JSONObject set(HttpServletRequest request){
        HttpSession session = request.getSession();
        logger.info("收到请求，session="+session);

        session.setAttribute("challenge", "123456");
        session.setAttribute("time", System.currentTimeMillis());
        session.setAttribute("server", port);
        JSONObject data = new JSONObject();
        Enumeration<String> iterator =  session.getAttributeNames();
        while(iterator.hasMoreElements()){
            String key = iterator.nextElement();
            logger.info("AttributeName="+key);
            data.put(key, session.getAttribute(key));
        }

        JSONObject result = new JSONObject();
        result.put("code", 0);
        result.put("serverPort", port);
        result.put("sessionId", session.getId());
        result.put("data", data);
        logger.info("返回的报文="+result);
        return result;
    }

    @RequestMapping(value = "/get")
    private JSONObject get(HttpServletRequest request){
        JSONObject result = new JSONObject();

        HttpSession session = request.getSession(false);
        logger.info("收到请求，session="+session);

        if(session==null){
            result.put("code", 400);
            result.put("msg", "不存在cookie!");
            return result;
        }

        JSONObject data = new JSONObject();
        Enumeration<String> iterator =  session.getAttributeNames();
        while(iterator.hasMoreElements()){
            String key = iterator.nextElement();
            logger.info("AttributeName="+key);
            data.put(key, session.getAttribute(key));
        }

        result.put("code", 0);
        result.put("serverPort", port);
        result.put("sessionId", session.getId());
        result.put("data", data);
        logger.info("返回的报文="+result);
        return result;
    }
}
